<template>
  <div class="main">
    <div class="main-title">商品入库</div>
    <div class="main-content">
      <div class="content-wrapper">
        <div class="tools-wrapper">
          <el-row>
            <el-col :span="16">
              <el-button type="primary" v-if="hasPermission('1004020202')" @click="add">添加</el-button>
            </el-col>
            <el-col :span="8" class="flex-box">
              <el-input class="f1" v-model="queryForm.instockNum" placeholder="请输入入库单号"></el-input>
              <el-select class="f1" placeholder="请选择入库类型" v-model="queryForm.instockType" clearable>
                <el-option v-for="(item, index) in instockTypes.TypeArray" :key="index" :value="item.KeyId" :label="item.Value"></el-option>
              </el-select>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-col>
          </el-row>
        </div>
        <div class="table-wrapper">
          <el-table :data="tableData" border stripe v-loading="$store.state.isLoading">
            <el-table-column label="序号" width="60" fixed="left" align="center">
              <template slot-scope="scope">
                {{(page-1)*pageSize + scope.$index + 1}}
              </template>
            </el-table-column>
            <el-table-column label="入库单号" fixed="left" prop="instockNum" align="center"></el-table-column>
            <el-table-column label="入库类型" align="center">
              <template slot-scope="scope">
                {{instockTypes.Types[scope.row.instockType]}}
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="remark" align="center"></el-table-column>
            <el-table-column label="创建人" prop="creator" align="center"></el-table-column>
            <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
            <el-table-column fixed="right" label="操作" width="150"  align="center">
              <template slot-scope="scope">
                <el-button type="text" size="mini" @click="check(scope)" v-if="hasPermission('1004020203')">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <v-pagination v-if="total" :pg="page" :size="pageSize" :total="total" :views="5" @page-change="pageChange" @size-change="sizeChange"></v-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { formatDate } from '@/utils/date.js'
import { instockTypes } from '@/data/enum.js'
export default {
  data () {
    return {
      queryForm: {
        instockNum: '',
        instockType: ''
      },
      instockTypes: instockTypes, // 入库类型
      tableData: [],
      page: Number(this.$route.query.pg) || 1,
      pageSize: Number(this.$route.query.size) || 10,
      total: 0
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    // 获取库存信息
    getData () {
      this.$store.commit('setLoading', true)
      this.$http.post(this.$api + '/stock/getStockInRecord', {
        instockNum: this.queryForm.instockNum || '',
        instockType: this.queryForm.instockType || '',
        page: this.page,
        pageSize: this.pageSize
      }).then(res => {
        if (res.responseCode === '0000') {
          res.data.list.forEach(item => {
            item.createTime = formatDate(new Date(item.createTime), 'yyyy-MM-dd hh:mm:ss')
          })
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error({
            duration: this.$store.state.duration,
            message: res.responseMsg
          })
        }
        this.$store.commit('setLoading', false)
      }).catch(err => {
        console.error(err)
        this.$store.commit('setLoading', false)
      })
    },
    // 添加
    add () {
      this.$router.push({
        path: '/stock/addImportInfo',
        query: {
          pg: this.page,
          size: this.pageSize
        }
      })
    },
    // 查看详情
    check (scope) {
      this.$router.push({
        path: '/stock/importDetail',
        query: {
          inRecordId: scope.row.inRecordId,
          pg: this.page,
          size: this.pageSize
        }
      })
    },
    // 分页
    sizeChange (size) {
      this.pageSize = size
      this.getData()
    },
    pageChange (page) {
      this.page = page
      this.getData()
    },
    search () {
      this.page = 1
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.flex-box {
  display: flex;
  justify-content: space-between;
  .f1 {
    margin-right: 10px;
    flex: 1;
  }
}
.upload-result-box {
  margin: 20px 0;
  padding: 10px;
  border: solid 1px #e5e5e5;
  .result-list {
    margin: 0;
    list-style-type: none;
    li {
      height: 20px;
      line-height: 20px;
      font-size: 12px;
    }
  }
}
</style>
